/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wasolicitudesfiec.Administracion.Usuarios.dataproviders;

import com.sun.data.provider.DataProviderException;
import com.sun.data.provider.FieldKey;
import com.sun.data.provider.RowKey;
import com.sun.data.provider.impl.AbstractTableDataProvider;
import com.sun.data.provider.impl.IndexRowKey;
import entidad.Persona;
import entidad.Rol;
import java.util.Vector;
import wasolicitudesfiec.registros.Rg_Solicitud;

/**
 *
 * @author Administrador
 */
public class UsuarioDataProvider extends AbstractTableDataProvider {

    private Vector<Persona> listaUsuarios;

    public UsuarioDataProvider() {
        agregarKey("id", "Identificador");
        agregarKey("usuario", "Usuario");
        agregarKey("nombre", "Nombre");
        agregarKey("roles", "Roles");
        agregarKey("correo", "Correo");
    }

    public void setListaUsuarios(Vector<Persona> listaUsuarios) {
        this.listaUsuarios = listaUsuarios;
    }

    private void agregarKey(String key_string, String display_name) {
        FieldKey key;
        key = new FieldKey(key_string);
        key.setDisplayName(display_name);
        addFieldKey(key);
    }

    @Override
    public Class getType(FieldKey fieldKey) throws DataProviderException {
        return String.class;
    }

    @Override
    public boolean isReadOnly(FieldKey fieldKey) throws DataProviderException {
        return true;
    }

    @Override
    public int getRowCount() throws DataProviderException {
        if (listaUsuarios != null) {
            return listaUsuarios.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getValue(FieldKey fieldKey, RowKey row) throws DataProviderException {
        int indice = ((IndexRowKey) row).getIndex();
		if (fieldKey==null)
			return "";
        String fieldId = fieldKey.getFieldId();
        Persona usuario = listaUsuarios.get(indice);
//        System.out.println("index:" + indice + " fieldKey.getFieldId():" + fieldId);
        if (fieldId.equals("id")){
            return usuario.getIdPersona();
        }else if (fieldId.equals("usuario")) {
            return usuario.getUsuario();
        } else if (fieldId.equals("nombre")) {
            return usuario.toString();
        } else if (fieldId.equals("roles")) {
            String roles = null;
            if (usuario.getRoles() != null) {
//				System.out.println("usuario: "+usuario.toString());
                for (Rol rol : usuario.getRoles()) {
//					System.out.println("rol: "+rol.toString());
                    if (roles == null) {
                        roles = rol.getRol();
                    } else {
                        roles = roles + ", " + rol.getRol();
                    }
                }
            }
            return roles;
        } else if (fieldId.equals("correo")) {
            return usuario.getCorreo();
        } else {
            return fieldId + indice;
        }

    }

    @Override
    public void setValue(FieldKey fieldKey, RowKey row, Object value) throws DataProviderException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean canInsertRow(RowKey beforeRow) throws DataProviderException {
        return false;
    }

    @Override
    public RowKey insertRow(RowKey beforeRow) throws DataProviderException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean canAppendRow() throws DataProviderException {
        return false;
    }

    @Override
    public RowKey appendRow() throws DataProviderException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean canRemoveRow(RowKey row) throws DataProviderException {
        return false;
    }

    @Override
    public void removeRow(RowKey row) throws DataProviderException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
